package com.shiguiwu.springmybatis.spring.cycle;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @description: 方法之前拦截
 * @author: stone
 * @date: Created by 2021/4/28 23:20
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.cycle
 */
//@Component
public class MethodBeforeInterceptor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("mather".equalsIgnoreCase(beanName)) {
            //创建代理对象
            ProxyFactory proxyFactory = new ProxyFactory(bean);
//            proxyFactory.addAdvice((Method m, Object[] args, @Nullable Object o) -> System.out.println("你好，mather"));
            proxyFactory.addAdvice((MethodBeforeAdvice) (method, objects, o) -> System.out.println("你好！！，mather"));
            return proxyFactory.getProxy();
        }
        return bean;
    }
}
